<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:xs="http://www.w3.org/2001/XMLSchema" 
	xmlns:aorml="http://AOR-AppML.org">
	<xsl:output name="sql" method="text" />
	<xsl:output name="php" method="text" />
	<xsl:variable name="break"><xsl:text>
</xsl:text></xsl:variable>
    <xsl:function name="aorml:upperFirst" as="xs:string?">
        <xsl:param name="trans" as="xs:string?"></xsl:param>
        <xsl:sequence select="concat(upper-case(substring($trans,1,1)),substring($trans,2))"/>
    </xsl:function>
    <xsl:function name="aorml:lowerFirst" as="xs:string?">
        <xsl:param name="trans" as="xs:string?"></xsl:param>
        <xsl:sequence select="concat(lower-case(substring($trans,1,1)),substring($trans,2))"/>
    </xsl:function>
	<xsl:template match="/">
		<xsl:call-template name="generateSQL" />
		<xsl:call-template name="generateZendDbCode" />
		<xsl:call-template name="generateZendControllers" />
		<xsl:call-template name="generateZendForms" />
	</xsl:template>

	<xsl:template name="generateSQL">
		<xsl:result-document href="out.sql" format="sql">
			<xsl:for-each select="/aorml:ApplicationModel/aorml:ObjectType">
				<xsl:text>CREATE TABLE IF NOT EXISTS `</xsl:text>
				<xsl:value-of select="@name"></xsl:value-of>
				<xsl:text>`(</xsl:text>
				<xsl:for-each select="aorml:Attribute">
				    <xsl:text>`</xsl:text>
				    <xsl:value-of select="@name"></xsl:value-of>
				    <xsl:text>`</xsl:text>			    
                    <xsl:text> </xsl:text>
				    <xsl:call-template name="getSQLType">
				        <xsl:with-param name="aorType" select="@type"></xsl:with-param>
				    </xsl:call-template>
				    <xsl:if test="@isStandardID = 'true'">
				    <xsl:text> PRIMARY KEY</xsl:text>
				    </xsl:if>
				    <xsl:if test="position() != last()">
				        <xsl:text>, </xsl:text>
				    </xsl:if>
				</xsl:for-each>
				<xsl:call-template name="getForeignKeys">
				    <xsl:with-param name="table" select="@name"></xsl:with-param>
				</xsl:call-template>
				<xsl:text>) ENGINE=INNODB;</xsl:text>
				<xsl:value-of select="$break"></xsl:value-of>
			</xsl:for-each>
			<xsl:call-template name="getManyToManySQL"></xsl:call-template>
		</xsl:result-document>
    </xsl:template>
		
    <xsl:template name="generateZendDbCode">
        <xsl:for-each select="/aorml:ApplicationModel/aorml:ObjectType">
            <xsl:call-template name="generateZendDbTable">
                <xsl:with-param name="table" select="@name" />
                <xsl:with-param name="primaryKey" select="aorml:Attribute/@name[../@isStandardID = 'true']" />
            </xsl:call-template>
            <xsl:call-template name="generateZendDbTableRow">
                <xsl:with-param name="table" select="@name" />
            </xsl:call-template>
        </xsl:for-each>
        <xsl:call-template name="genManyToManyZendDb"></xsl:call-template>
	</xsl:template>

    <xsl:template name="generateZendDbTable">
        <xsl:param name="table"></xsl:param>
        <xsl:param name="primaryKey"></xsl:param>
        <xsl:result-document href="application/models/DbTable/{$table}.php"	format="php">
	        <xsl:text>&lt;?php</xsl:text>
            <xsl:value-of select="$break" />
	        <xsl:text>class Application_Model_DbTable_</xsl:text>
	        <xsl:value-of select="$table" />
	        <xsl:text> extends Zend_Db_Table_Abstract</xsl:text>
	        <xsl:value-of select="$break" />
	        <xsl:text>{</xsl:text>
	        <xsl:value-of select="$break" />
	        <xsl:text>protected $_name = '</xsl:text><xsl:value-of select="$table"/><xsl:text>';</xsl:text>
	        <xsl:value-of select="$break" />
	        <xsl:text>protected $_primary = '</xsl:text><xsl:value-of select="$primaryKey" /><xsl:text>';</xsl:text>
	        <xsl:value-of select="$break" />
	        <xsl:text>protected $_sequence = false;</xsl:text>
	        <xsl:value-of select="$break" />
	        <xsl:text>protected $_rowClass = 'Application_Model_DbTable_</xsl:text>
	        <xsl:value-of select="$table"/>
	        <xsl:text>Row';</xsl:text>
	        <xsl:value-of select="$break" />
	        <xsl:call-template name="dependentTables">
	           <xsl:with-param name="table" select="$table" />
	        </xsl:call-template>	        
	        <xsl:value-of select="$break" />
	        <xsl:call-template name="referenceMap">
               <xsl:with-param name="table" select="$table" />
            </xsl:call-template>            
            <xsl:value-of select="$break" />
	        <xsl:text>}</xsl:text>
        </xsl:result-document>
	</xsl:template>
	
    <xsl:template name="generateZendDbTableRow">
        <xsl:param name="table"></xsl:param>
        <xsl:result-document href="application/models/DbTable/{$table}Row.php" format="php">
            <xsl:text>&lt;?php</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>class Application_Model_DbTable_</xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>Row extends Zend_Db_Table_Row_Abstract</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>{</xsl:text>
            <xsl:value-of select="$break"></xsl:value-of>
            <xsl:for-each select="//aorml:ObjectType[@name=$table]/aorml:Attribute">
                <xsl:text>public function get</xsl:text>
                <xsl:value-of select="aorml:upperFirst(@name)"></xsl:value-of>
                <xsl:text>() {</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                <xsl:text>return $this-></xsl:text>
                <xsl:value-of select="@name"></xsl:value-of>
                <xsl:text>;</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                <xsl:text>}</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                
                <xsl:text>public function set</xsl:text>
                <xsl:value-of select="aorml:upperFirst(@name)"></xsl:value-of>
                <xsl:text>($</xsl:text>
                <xsl:value-of select="@name"></xsl:value-of>
                <xsl:text>) {</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                <xsl:text>$this-></xsl:text>
                <xsl:value-of select="@name"></xsl:value-of>
                <xsl:text> = $</xsl:text>
                <xsl:value-of select="@name"></xsl:value-of>
                <xsl:text>;</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                <xsl:text>}</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
            </xsl:for-each>
            <xsl:text>}</xsl:text>
        </xsl:result-document>
    </xsl:template>
    
    <xsl:template name="genManyToManyZendDb">
        <xsl:for-each select="//aorml:BiNavigableAssociation
            [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']
            [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']
            |//aorml:NavigableAssociation
            [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']
            [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']">
            <xsl:variable name="domainTable" select="@domain"></xsl:variable>
            <xsl:variable name="rangeTable" select="@range"></xsl:variable>     
            <xsl:variable name="domainPrimary" select="//aorml:ObjectType[@name=$domainTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>       
            <xsl:variable name="rangePrimary" select="//aorml:ObjectType[@name=$rangeTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>
            <xsl:variable name="manyTable">
                <xsl:value-of select="@name"></xsl:value-of>
                <xsl:if test="not(@name)">
                    <xsl:value-of select="@domain"></xsl:value-of>
                    <xsl:value-of select="@range"></xsl:value-of>
                </xsl:if>
            </xsl:variable>
            <xsl:variable name="domainRole">
                <xsl:value-of select="@domainRoleName"></xsl:value-of>
                <xsl:if test="not(@domainRoleName)">
                    <xsl:value-of select="aorml:lowerFirst(@domain)"></xsl:value-of>
                </xsl:if>
            </xsl:variable>
            <xsl:variable name="rangeRole">
                <xsl:value-of select="@rangeRoleName"></xsl:value-of>
                <xsl:if test="not(@rangeRoleName)">
                    <xsl:value-of select="aorml:lowerFirst(@range)"></xsl:value-of>
                </xsl:if>
            </xsl:variable>
            <xsl:result-document href="application/models/DbTable/{$manyTable}.php" format="php">
                <xsl:text>&lt;?php</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>class Application_Model_DbTable_</xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text> extends Zend_Db_Table_Abstract</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>{</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>protected $_name = '</xsl:text><xsl:value-of select="$manyTable"/><xsl:text>';</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>protected $_primary = array('</xsl:text>
                <xsl:value-of select="$domainRole" />
                <xsl:text>', '</xsl:text>
                <xsl:value-of select="$rangeRole" />
                <xsl:text>');</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>protected $_sequence = false;</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>protected $_rowClass = 'Application_Model_DbTable_</xsl:text>
                <xsl:value-of select="$manyTable"/>
                <xsl:text>Row';</xsl:text>
                <xsl:value-of select="$break" />   
                <xsl:text>protected $_referenceMap = array(</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>'</xsl:text>   
                <xsl:value-of select="$domainTable" />
                <xsl:text>' => array(</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>'columns' => '</xsl:text>
                <xsl:value-of select="$domainRole" />
                <xsl:text>',</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>'refTableClass' => '</xsl:text>
                <xsl:value-of select="$domainTable" />
                <xsl:text>',</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>'refColumn' => '</xsl:text>
                <xsl:value-of select="$domainPrimary" />
                <xsl:text>'</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>),</xsl:text>
                
                <xsl:value-of select="$break" />
                <xsl:text>'</xsl:text>   
                <xsl:value-of select="$rangeTable" />
                <xsl:text>' => array(</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>'columns' => '</xsl:text>
                <xsl:value-of select="$rangeRole" />
                <xsl:text>',</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>'refTableClass' => '</xsl:text>
                <xsl:value-of select="$rangeTable" />
                <xsl:text>',</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>'refColumn' => '</xsl:text>
                <xsl:value-of select="$rangePrimary" />
                <xsl:text>'</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>)</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>);</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}</xsl:text>
            </xsl:result-document>
                        
            <!-- DBTable_Row  -->   
            <xsl:result-document href="application/models/DbTable/{$manyTable}Row.php" format="php">
            <xsl:text>&lt;?php</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>class Application_Model_DbTable_</xsl:text>
            <xsl:value-of select="$manyTable" />
            <xsl:text>Row extends Zend_Db_Table_Row_Abstract</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>{</xsl:text>
            <xsl:value-of select="$break"></xsl:value-of>
            
                <xsl:text>public function get</xsl:text>
                <xsl:value-of select="aorml:upperFirst($domainRole)"></xsl:value-of>
                <xsl:text>() {</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                <xsl:text>return $this-></xsl:text>
                <xsl:value-of select="aorml:lowerFirst($domainRole)"></xsl:value-of>
                <xsl:text>;</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                <xsl:text>}</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                
                <xsl:text>public function set</xsl:text>
                <xsl:value-of select="aorml:upperFirst($domainRole)"></xsl:value-of>
                <xsl:text>($</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($domainRole)"></xsl:value-of>
                <xsl:text>) {</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                <xsl:text>$this-></xsl:text>
                <xsl:value-of select="aorml:lowerFirst($domainRole)"></xsl:value-of>
                <xsl:text> = $</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($domainRole)"></xsl:value-of>
                <xsl:text>;</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                <xsl:text>}</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                
                <xsl:text>public function get</xsl:text>
                <xsl:value-of select="aorml:upperFirst($rangeRole)"></xsl:value-of>
                <xsl:text>() {</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                <xsl:text>return $this-></xsl:text>
                <xsl:value-of select="aorml:lowerFirst($rangeRole)"></xsl:value-of>
                <xsl:text>;</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                <xsl:text>}</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                
                <xsl:text>public function set</xsl:text>
                <xsl:value-of select="aorml:upperFirst($rangeRole)"></xsl:value-of>
                <xsl:text>($</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($rangeRole)"></xsl:value-of>
                <xsl:text>) {</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                <xsl:text>$this-></xsl:text>
                <xsl:value-of select="aorml:lowerFirst($rangeRole)"></xsl:value-of>
                <xsl:text> = $</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($rangeRole)"></xsl:value-of>
                <xsl:text>;</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
                <xsl:text>}</xsl:text>
                <xsl:value-of select="$break"></xsl:value-of>
            
            <xsl:text>}</xsl:text>
        </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template name="generateZendControllers">
        <xsl:for-each select="/aorml:ApplicationModel/aorml:ObjectType">
            <xsl:call-template name="generateTableController">
                <xsl:with-param name="table" select="@name" />
                <xsl:with-param name="primaryKey" select="aorml:Attribute/@name[../@isStandardID = 'true']" />
            </xsl:call-template>
            <xsl:call-template name="generateViews">
                <xsl:with-param name="table" select="@name" />
                <xsl:with-param name="primaryKey" select="aorml:Attribute/@name[../@isStandardID = 'true']" />
            </xsl:call-template>            
        </xsl:for-each>        
        <xsl:call-template name="generateManyToManyControllers"></xsl:call-template>
        <xsl:call-template name="generateManyToManyViews"></xsl:call-template>
    </xsl:template>
    
    <xsl:template name="generateTableController">
        <xsl:param name="table"></xsl:param>
        <xsl:param name="primaryKey"/>
        <xsl:result-document href="application/controllers/{$table}Controller.php" format="php">
            <xsl:text>&lt;?php</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>class </xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>Controller extends Zend_Controller_Action</xsl:text>            
            <xsl:value-of select="$break" />
            <xsl:text>{</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>public function init()</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>{</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>/* Initialize action controller here */</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:value-of select="$break" />
            
            <!-- IndexController -->
            <xsl:text>public function indexAction()</xsl:text>            
            <xsl:value-of select="$break" />
            <xsl:text>{</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$this->view->title = "</xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>";</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$this->view->headTitle($this->view->title);</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$</xsl:text>            
            <xsl:value-of select="$table" />
            <xsl:text> = new Application_Model_DbTable_</xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>();</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$this->view-></xsl:text>            
            <xsl:value-of select="$table" />
            <xsl:text> = $</xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>->fetchAll();</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}//index</xsl:text>            
            <xsl:value-of select="$break" />
            
            
            <!-- AddController -->
            <xsl:text>public function addAction()</xsl:text>            
            <xsl:value-of select="$break" />
            <xsl:text>{</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$this->view->title = "Add new </xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>";</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$this->view->headTitle($this->view->title);</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$form = new Application_Form_</xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>();</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$form->submit->setLabel('Add');</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$this->view->form = $form;</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>if ($this->getRequest()->isPost()) {</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$formData = $this->getRequest()->getPost();</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>if ($form->isValid($formData)) {</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$rowset = new Application_Model_DbTable_</xsl:text>
            <xsl:value-of select="$table"></xsl:value-of>
            <xsl:text>();</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$row = $rowset->createRow();</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:for-each select="//aorml:ObjectType[@name=$table]/aorml:Attribute">
                <xsl:text>$row->set</xsl:text>
                <xsl:value-of select="aorml:upperFirst(@name)"></xsl:value-of>
                <xsl:text>($form->getValue('</xsl:text>
                <xsl:value-of select="aorml:lowerFirst(@name)"></xsl:value-of>
                <xsl:text>'));</xsl:text>
                <xsl:value-of select="$break" />
            </xsl:for-each>
            <xsl:text>$row->save();</xsl:text>
            <xsl:text>$this->_helper->redirector('index');</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>} else {</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$form->populate($formData);</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}//add</xsl:text>
            <xsl:value-of select="$break" />
            
            <!-- EditController -->
            <xsl:text>public function editAction()</xsl:text>            
            <xsl:value-of select="$break" />
            <xsl:text>{</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$this->view->title = "Edit </xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>";</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$this->view->headTitle($this->view->title);</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$form = new Application_Form_</xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>();</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$form->submit->setLabel('Save');</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$this->view->form = $form;</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>if ($this->getRequest()->isPost()) {</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$formData = $this->getRequest()->getPost();</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>if ($form->isValid($formData)) {</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$rowset = new Application_Model_DbTable_</xsl:text>
            <xsl:value-of select="$table"></xsl:value-of>
            <xsl:text>();</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$row = $rowset->fetchRow($rowset->select()->where('</xsl:text>
            <xsl:value-of select="$primaryKey" />
            <xsl:text> = ?', </xsl:text>
            <xsl:text>$form->getValue('</xsl:text>
            <xsl:value-of select="$primaryKey"/>
            <xsl:text>')));</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:for-each select="//aorml:ObjectType[@name=$table]/aorml:Attribute">
                <xsl:text>$row->set</xsl:text>
                <xsl:value-of select="aorml:upperFirst(@name)"></xsl:value-of>
                <xsl:text>($form->getValue('</xsl:text>
                <xsl:value-of select="aorml:lowerFirst(@name)"></xsl:value-of>
                <xsl:text>'));</xsl:text>
                <xsl:value-of select="$break" />
            </xsl:for-each>
            <xsl:text>$row->save();</xsl:text>
            <xsl:value-of select="$break" />            
            <xsl:text>$this->_helper->redirector('index');</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>} else {</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$form->populate($formData);</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}</xsl:text>
            <xsl:text> else {</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$</xsl:text>
            <xsl:value-of select="$primaryKey"></xsl:value-of>
            <xsl:text> = $this->_getParam('</xsl:text>
            <xsl:value-of select="$primaryKey"></xsl:value-of>
            <xsl:text>', 0);</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>if($</xsl:text>
            <xsl:value-of select="$primaryKey"></xsl:value-of>
            <xsl:text>) {</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$rowset = new Application_Model_DbTable_</xsl:text>
            <xsl:value-of select="$table"></xsl:value-of>
            <xsl:text>();</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$row = $rowset->fetchRow($rowset->select()->where('</xsl:text>
            <xsl:value-of select="$primaryKey" />
            <xsl:text> = ?', $</xsl:text>
            <xsl:value-of select="$primaryKey" />
            <xsl:text>));</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$form->populate($row->toArray());</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}//edit</xsl:text>            
            <xsl:value-of select="$break" />
            
            <!-- DeleteController -->
            <xsl:text>public function deleteAction()</xsl:text>            
            <xsl:value-of select="$break" />
            <xsl:text>{</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$this->view->title = "Delete from </xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>";</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$this->view->headTitle($this->view->title);</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>if ($this->getRequest()->isPost()) {</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$del = $this->getRequest()->getPost('del');</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>if ($del == 'Yes') {</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$</xsl:text>
            <xsl:value-of select="$primaryKey" />
            <xsl:text> = $this->getRequest()->getPost('</xsl:text>
            <xsl:value-of select="$primaryKey" />
            <xsl:text>');</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$rowset = new Application_Model_DbTable_</xsl:text>
            <xsl:value-of select="$table"></xsl:value-of>
            <xsl:text>();</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$where = $rowset->getAdapter()->quoteInto('</xsl:text>
            <xsl:value-of select="$primaryKey" />
            <xsl:text> = ?', </xsl:text>
            <xsl:text>$</xsl:text>
            <xsl:value-of select="$primaryKey" />
            <xsl:text>);</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$rowset->delete($where);</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$this->_helper->redirector('index');</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>} else {</xsl:text>
             <xsl:value-of select="$break" />
            <xsl:text>$</xsl:text>
            <xsl:value-of select="$primaryKey"></xsl:value-of>
            <xsl:text> = $this->_getParam('</xsl:text>
            <xsl:value-of select="$primaryKey"></xsl:value-of>
            <xsl:text>', 0);</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$rowset =  new Application_Model_DbTable_</xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>();</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$row = $rowset->fetchRow($rowset->select()->where('</xsl:text>
            <xsl:value-of select="$primaryKey" />
            <xsl:text> = ?', $</xsl:text>
            <xsl:value-of select="$primaryKey" />
            <xsl:text>));</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$this->view-></xsl:text>            
            <xsl:value-of select="$table" />
            <xsl:text> = $row;</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}</xsl:text>            
            <xsl:value-of select="$break" />
            
            <xsl:text>}//delete</xsl:text>            
            <xsl:value-of select="$break" />
            
            
            <xsl:value-of select="$break" />
            <xsl:text>}//class</xsl:text>
        </xsl:result-document>
    </xsl:template>
    
    <xsl:template name="generateManyToManyControllers">
        <xsl:for-each select="//aorml:BiNavigableAssociation
            [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']
            [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']
            |//aorml:NavigableAssociation
            [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']
            [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']">
            <xsl:variable name="domainTable" select="@domain"></xsl:variable>
            <xsl:variable name="rangeTable" select="@range"></xsl:variable>     
            <xsl:variable name="domainPrimary" select="//aorml:ObjectType[@name=$domainTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>       
            <xsl:variable name="rangePrimary" select="//aorml:ObjectType[@name=$rangeTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>
            <xsl:variable name="manyTable">
                <xsl:value-of select="@name"></xsl:value-of>
                <xsl:if test="not(@name)">
                    <xsl:value-of select="@domain"></xsl:value-of>
                    <xsl:value-of select="@range"></xsl:value-of>
                </xsl:if>
            </xsl:variable>
            <xsl:variable name="domainRole">
                <xsl:value-of select="@domainRoleName"></xsl:value-of>
                <xsl:if test="not(@domainRoleName)">
                    <xsl:value-of select="aorml:lowerFirst(@domain)"></xsl:value-of>
                </xsl:if>
            </xsl:variable>
            <xsl:variable name="rangeRole">
                <xsl:value-of select="@rangeRoleName"></xsl:value-of>
                <xsl:if test="not(@rangeRoleName)">
                    <xsl:value-of select="aorml:lowerFirst(@range)"></xsl:value-of>
                </xsl:if>
            </xsl:variable>
            <xsl:result-document href="application/controllers/{$manyTable}Controller.php" format="php">
                <xsl:text>&lt;?php</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>class </xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text>Controller extends Zend_Controller_Action</xsl:text>            
                <xsl:value-of select="$break" />
                <xsl:text>{</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>public function init()</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>{</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>/* Initialize action controller here */</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>}</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:value-of select="$break" />
                
                <!-- IndexController -->
                <xsl:text>public function indexAction()</xsl:text>            
                <xsl:value-of select="$break" />
                <xsl:text>{</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$this->view->title = "</xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text>";</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$this->view->headTitle($this->view->title);</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$</xsl:text>            
                <xsl:value-of select="$manyTable" />
                <xsl:text> = new Application_Model_DbTable_</xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text>();</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$this->view-></xsl:text>            
                <xsl:value-of select="$manyTable" />
                <xsl:text> = $</xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text>->fetchAll();</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>}//index</xsl:text>            
                <xsl:value-of select="$break" />
                
               <!-- AddController -->
                <xsl:text>public function addAction()</xsl:text>            
                <xsl:value-of select="$break" />
                <xsl:text>{</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$this->view->title = "Add new </xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text>";</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$this->view->headTitle($this->view->title);</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$form = new Application_Form_</xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text>();</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$form->submit->setLabel('Add');</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$this->view->form = $form;</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>if ($this->getRequest()->isPost()) {</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$formData = $this->getRequest()->getPost();</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>if ($form->isValid($formData)) {</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$rowset = new Application_Model_DbTable_</xsl:text>
                <xsl:value-of select="$manyTable"></xsl:value-of>
                <xsl:text>();</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$row = $rowset->createRow();</xsl:text>
                <xsl:value-of select="$break" />
                
                <xsl:text>$row->set</xsl:text>
                <xsl:value-of select="aorml:upperFirst($domainRole)"></xsl:value-of>
                <xsl:text>($form->getValue('</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($domainRole)"></xsl:value-of>
                <xsl:text>'));</xsl:text>
                <xsl:value-of select="$break" />
                
                <xsl:text>$row->set</xsl:text>
                <xsl:value-of select="aorml:upperFirst($rangeRole)"></xsl:value-of>
                <xsl:text>($form->getValue('</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($rangeRole)"></xsl:value-of>
                <xsl:text>'));</xsl:text>
                <xsl:value-of select="$break" />
            
                <xsl:text>$row->save();</xsl:text>
                <xsl:text>$this->_helper->redirector('index');</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>} else {</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$form->populate($formData);</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>}</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>}</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>}//add</xsl:text>
                <xsl:value-of select="$break" />
                
                           
            <xsl:value-of select="$break" />
            <xsl:text>}</xsl:text>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template name="generateViews">
        <xsl:param name="table"></xsl:param>
        <xsl:param name="primaryKey"></xsl:param>
        <!-- Index.phtml -->
        <xsl:result-document href="application/views/scripts/{$table}/index.phtml" format="php">
            <xsl:text>&lt;p&gt;&lt;a href="&lt;?php echo $this->url(array('controller'=>'</xsl:text>
            <xsl:value-of select="$table"/>
            <xsl:text>','action'=>'add'));?&gt;">Add new </xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>&lt;/a&gt;&lt;/p&gt;</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>&lt;table&gt;&lt;tr&gt;</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:for-each select="//aorml:ObjectType[@name=$table]/aorml:Attribute">
                <xsl:text>&lt;th&gt;</xsl:text>
                <xsl:value-of select="@name"></xsl:value-of>
                <xsl:text>&lt;/th&gt;</xsl:text>
            </xsl:for-each>
            <xsl:text>&lt;th&gt; &amp;nbsp; &lt;/th&gt;</xsl:text>
            <xsl:text>&lt;/tr&gt;</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>&lt;?php foreach($this-></xsl:text>
            <xsl:value-of select="$table"/>
            <xsl:text> as $row) { ?&gt;</xsl:text>
            <xsl:value-of select="$break" />       
            <xsl:text>&lt;tr&gt;</xsl:text>
            <xsl:for-each select="//aorml:ObjectType[@name=$table]/aorml:Attribute">
                <xsl:text>&lt;td&gt; &lt;?php echo $this->escape($row->get</xsl:text>
                <xsl:value-of select="aorml:upperFirst(@name)"></xsl:value-of>
                <xsl:text>()); ?&gt;&lt;/td&gt;</xsl:text>
                <xsl:value-of select="$break" />
            </xsl:for-each>
            <xsl:text>&lt;td&gt; &lt;a href="&lt;?php echo $this->url(array(</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>'controller'=> '</xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>', 'action'=>'edit', '</xsl:text>
            <xsl:value-of select="$primaryKey" />
            <xsl:text>' => $row->get</xsl:text>
            <xsl:value-of select="aorml:upperFirst($primaryKey)" />
            <xsl:text>()));?>">Edit&lt;/a></xsl:text>
            <xsl:text>&lt;/td&gt;</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>&lt;td&gt; &lt;a href="&lt;?php echo $this->url(array(</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>'controller'=> '</xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>', 'action'=>'delete', '</xsl:text>
            <xsl:value-of select="$primaryKey" />
            <xsl:text>' => $row->get</xsl:text>
            <xsl:value-of select="$primaryKey" />
            <xsl:text>()));?>">Delete&lt;/a></xsl:text>
            <xsl:text>&lt;/td&gt;</xsl:text>
            <xsl:text>&lt;/tr&gt;</xsl:text>  
            <xsl:value-of select="$break" /> 
            <xsl:text>&lt;?php } ?&gt;</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>&lt;/table&gt;</xsl:text>
        </xsl:result-document>
        <!-- add.phtml -->
        <xsl:result-document href="application/views/scripts/{$table}/add.phtml" format="php">
            <xsl:text>&lt;?php echo $this->form ;?></xsl:text>
        </xsl:result-document>
        <!-- edit.phtml -->
        <xsl:result-document href="application/views/scripts/{$table}/edit.phtml" format="php">
            <xsl:text>&lt;?php echo $this->form ;?></xsl:text>
        </xsl:result-document>
        <!-- delete.phtml -->
        <xsl:result-document href="application/views/scripts/{$table}/delete.phtml" format="php">
            <xsl:text>&lt;p>Are you sure that you want to delete '</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:for-each select="//aorml:ObjectType[@name=$table]/aorml:Attribute">
                <xsl:text>&lt;?php echo $this-></xsl:text>
                <xsl:value-of select="$table" />
                <xsl:text>->get</xsl:text>
                <xsl:value-of select="aorml:upperFirst(@name)" />
                <xsl:text>(); ?>' </xsl:text>
                <xsl:value-of select="$break" />
            </xsl:for-each>
            <xsl:text>&lt;/p></xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>&lt;form action="&lt;?php echo $this->url(array('action'=>'delete')); ?>" method="post"></xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>&lt;div></xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>&lt;input type="hidden" name="</xsl:text>
            <xsl:value-of select="$primaryKey" />
            <xsl:text>" value="&lt;?php echo $this-></xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>->get</xsl:text>
            <xsl:value-of select="$primaryKey" />
            <xsl:text>(); ?>" /></xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>&lt;input type="submit" name="del" value="Yes" /></xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>&lt;input type="submit" name="del" value="No" /></xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>&lt;/div></xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>&lt;/form></xsl:text>
            <xsl:value-of select="$break" />
            
        </xsl:result-document>
    </xsl:template>
    
    <xsl:template name="generateManyToManyViews">
        <xsl:for-each select="//aorml:BiNavigableAssociation
            [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']
            [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']
            |//aorml:NavigableAssociation
            [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']
            [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']">
            <xsl:variable name="domainTable" select="@domain"></xsl:variable>
            <xsl:variable name="rangeTable" select="@range"></xsl:variable>     
            <xsl:variable name="domainPrimary" select="//aorml:ObjectType[@name=$domainTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>       
            <xsl:variable name="rangePrimary" select="//aorml:ObjectType[@name=$rangeTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>
            <xsl:variable name="manyTable">
                <xsl:value-of select="@name"></xsl:value-of>
                <xsl:if test="not(@name)">
                    <xsl:value-of select="@domain"></xsl:value-of>
                    <xsl:value-of select="@range"></xsl:value-of>
                </xsl:if>
            </xsl:variable>
            <xsl:variable name="domainRole">
                <xsl:value-of select="@domainRoleName"></xsl:value-of>
                <xsl:if test="not(@domainRoleName)">
                    <xsl:value-of select="aorml:lowerFirst(@domain)"></xsl:value-of>
                </xsl:if>
            </xsl:variable>
            <xsl:variable name="rangeRole">
                <xsl:value-of select="@rangeRoleName"></xsl:value-of>
                <xsl:if test="not(@rangeRoleName)">
                    <xsl:value-of select="aorml:lowerFirst(@range)"></xsl:value-of>
                </xsl:if>
            </xsl:variable>
            
            <!-- Index.phtml -->
            <xsl:result-document href="application/views/scripts/{$manyTable}/index.phtml" format="php">
                <xsl:text>&lt;p&gt;&lt;a href="&lt;?php echo $this->url(array('controller'=>'</xsl:text>
                <xsl:value-of select="$manyTable"/>
                <xsl:text>','action'=>'add'));?&gt;">Add new </xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text>&lt;/a&gt;&lt;/p&gt;</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>&lt;table&gt;&lt;tr&gt;</xsl:text>
                <xsl:value-of select="$break" />            
                <xsl:text>&lt;th&gt;</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($domainRole)"></xsl:value-of>
                <xsl:text>&lt;/th&gt;</xsl:text>                
                <xsl:text>&lt;th&gt;</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($rangeRole)"></xsl:value-of>
                <xsl:text>&lt;/th&gt;</xsl:text>            
                <xsl:text>&lt;th&gt; &amp;nbsp; &lt;/th&gt;</xsl:text>
                <xsl:text>&lt;/tr&gt;</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>&lt;?php foreach($this-></xsl:text>
                <xsl:value-of select="$manyTable"/>
                <xsl:text> as $row) { ?&gt;</xsl:text>
                <xsl:value-of select="$break" />       
                <xsl:text>&lt;tr&gt;</xsl:text>            
                <xsl:text>&lt;td&gt; &lt;?php echo $this->escape($row->get</xsl:text>
                <xsl:value-of select="aorml:upperFirst($domainRole)"></xsl:value-of>
                <xsl:text>()); ?&gt;&lt;/td&gt;</xsl:text>
                <xsl:value-of select="$break" />                
                <xsl:text>&lt;td&gt; &lt;?php echo $this->escape($row->get</xsl:text>
                <xsl:value-of select="aorml:upperFirst($rangeRole)"></xsl:value-of>
                <xsl:text>()); ?&gt;&lt;/td&gt;</xsl:text>
                <xsl:value-of select="$break" />            
                
                <xsl:value-of select="$break" />
                <xsl:text>&lt;td&gt; &lt;a href="&lt;?php echo $this->url(array(</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>'controller'=> '</xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text>', 'action'=>'delete', '</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($domainRole)" />
                <xsl:text>' => $row->get</xsl:text>
                <xsl:value-of select="aorml:upperFirst($domainRole)" />
                <xsl:text>(), '</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($rangeRole)" />
                <xsl:text>' => $row->get</xsl:text>
                <xsl:value-of select="aorml:upperFirst($rangeRole)" />
                <xsl:text>()));?>">Delete&lt;/a></xsl:text>
                <xsl:text>&lt;/td&gt;</xsl:text>
                <xsl:text>&lt;/tr&gt;</xsl:text>  
                <xsl:value-of select="$break" /> 
                <xsl:text>&lt;?php } ?&gt;</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>&lt;/table&gt;</xsl:text>
            </xsl:result-document>
            
            <!-- add.phtml -->
            <xsl:result-document href="application/views/scripts/{$manyTable}/add.phtml" format="php">
                <xsl:text>&lt;?php echo $this->form ;?></xsl:text>
            </xsl:result-document>
            
            <!-- edit.phtml -->
            <xsl:result-document href="application/views/scripts/{$manyTable}/edit.phtml" format="php">
                <xsl:text>&lt;?php echo $this->form ;?></xsl:text>
            </xsl:result-document>
            
            <!-- delete.phtml -->
            <xsl:result-document href="application/views/scripts/{$manyTable}/delete.phtml" format="php">
                <xsl:text>&lt;p>Are you sure that you want to delete '</xsl:text>
                <xsl:value-of select="$break" />                
                <xsl:text>&lt;?php echo $this-></xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text>->get</xsl:text>
                <xsl:value-of select="aorml:upperFirst($domainRole)" />
                <xsl:text>(); ?>' </xsl:text>
                <xsl:value-of select="$break" />                
                <xsl:text>&lt;?php echo $this-></xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text>->get</xsl:text>
                <xsl:value-of select="aorml:upperFirst(@rangeRole)" />
                <xsl:text>(); ?>' </xsl:text>
                <xsl:value-of select="$break" />      
                <xsl:text>&lt;/p></xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>&lt;form action="&lt;?php echo $this->url(array('action'=>'delete')); ?>" method="post"></xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>&lt;div></xsl:text>
                <xsl:value-of select="$break" />            
                <xsl:text>&lt;input type="hidden" name="</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($domainRole)" />
                <xsl:text>" value="&lt;?php echo $this-></xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text>->get</xsl:text>
                <xsl:value-of select="aorml:upperFirst($domainRole)" />
                <xsl:text>(); ?>" /></xsl:text>
                <xsl:value-of select="$break" />            
                <xsl:text>&lt;input type="hidden" name="</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($rangeRole)" />
                <xsl:text>" value="&lt;?php echo $this-></xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text>->get</xsl:text>
                <xsl:value-of select="aorml:upperFirst($rangeRole)" />
                <xsl:text>(); ?>" /></xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>&lt;input type="submit" name="del" value="Yes" /></xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>&lt;input type="submit" name="del" value="No" /></xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>&lt;/div></xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>&lt;/form></xsl:text>
                <xsl:value-of select="$break" />            
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template name="generateZendForms">
        <xsl:for-each select="/aorml:ApplicationModel/aorml:ObjectType">
            <xsl:call-template name="generateForm">
                <xsl:with-param name="table" select="@name" />
            </xsl:call-template>
        </xsl:for-each>
        <xsl:call-template name="generateZendManyForms"></xsl:call-template>
    </xsl:template>
    
    <xsl:template name="generateZendManyForms">
        <xsl:for-each select="//aorml:BiNavigableAssociation
            [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']
            [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']
            |//aorml:NavigableAssociation
            [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']
            [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']">
            <xsl:variable name="domainTable" select="@domain"></xsl:variable>
            <xsl:variable name="rangeTable" select="@range"></xsl:variable>     
            <xsl:variable name="domainPrimary" select="//aorml:ObjectType[@name=$domainTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>       
            <xsl:variable name="rangePrimary" select="//aorml:ObjectType[@name=$rangeTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>
            <xsl:variable name="manyTable">
                <xsl:value-of select="@name"></xsl:value-of>
                <xsl:if test="not(@name)">
                    <xsl:value-of select="@domain"></xsl:value-of>
                    <xsl:value-of select="@range"></xsl:value-of>
                </xsl:if>
            </xsl:variable>
            <xsl:variable name="domainRole">
                <xsl:value-of select="@domainRoleName"></xsl:value-of>
                <xsl:if test="not(@domainRoleName)">
                    <xsl:value-of select="aorml:lowerFirst(@domain)"></xsl:value-of>
                </xsl:if>
            </xsl:variable>
            <xsl:variable name="rangeRole">
                <xsl:value-of select="@rangeRoleName"></xsl:value-of>
                <xsl:if test="not(@rangeRoleName)">
                    <xsl:value-of select="aorml:lowerFirst(@range)"></xsl:value-of>
                </xsl:if>
            </xsl:variable>
            
            <xsl:result-document href="application/forms/{$manyTable}.php" format="php">
                <xsl:text>&lt;?php</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>class Application_Form_</xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text> extends Zend_Form</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>{</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>public function __construct($options = null)</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>{</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>parent::__construct($options);</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$this->setName('</xsl:text>
                <xsl:value-of select="$manyTable" />
                <xsl:text>');</xsl:text>
                <xsl:value-of select="$break" />
               <!-- domainKey -->
                <xsl:text>$</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($domainRole)" />
                <xsl:text> = new Zend_Form_Element_Text('</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($domainRole)" />
                <xsl:text>');</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($domainRole)" />
                <xsl:text>->setLabel('</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($domainRole)" />
                <xsl:text>')</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>->setRequired(true)</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>->addValidator('NotEmpty');</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$this->addElement($</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($domainRole)" />
                <xsl:text>);</xsl:text>
                <xsl:value-of select="$break" />            
                <!-- rangeKey -->
                <xsl:text>$</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($rangeRole)" />
                <xsl:text> = new Zend_Form_Element_Text('</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($rangeRole)" />
                <xsl:text>');</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($rangeRole)" />
                <xsl:text>->setLabel('</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($rangeRole)" />
                <xsl:text>')</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>->setRequired(true)</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>->addValidator('NotEmpty');</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$this->addElement($</xsl:text>
                <xsl:value-of select="aorml:lowerFirst($rangeRole)" />
                <xsl:text>);</xsl:text>
                <xsl:value-of select="$break" />                       
            
                <xsl:text>$submit = new Zend_Form_Element_Submit('submit');</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$submit->setAttrib('id', 'submitbutton');</xsl:text>   
                <xsl:value-of select="$break" />
                <xsl:text>$this->addElement($submit);</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>}</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>}</xsl:text>
            </xsl:result-document>            
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template name="generateForm">
        <xsl:param name="table"></xsl:param>
        <xsl:result-document href="application/forms/{$table}.php" format="php">
            <xsl:text>&lt;?php</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>class Application_Form_</xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text> extends Zend_Form</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>{</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>public function __construct($options = null)</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>{</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>parent::__construct($options);</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$this->setName('</xsl:text>
            <xsl:value-of select="$table" />
            <xsl:text>');</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:for-each select="//aorml:ObjectType[@name=$table]/aorml:Attribute">
                <xsl:text>$</xsl:text>
                <xsl:value-of select="@name" />
                <xsl:text> = new Zend_Form_Element_Text('</xsl:text>
                <xsl:value-of select="@name" />
                <xsl:text>');</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$</xsl:text>
                <xsl:value-of select="@name" />
                <xsl:text>->setLabel('</xsl:text>
                <xsl:value-of select="@name" />
                <xsl:text>')</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>->setRequired(true)</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>->addValidator('NotEmpty');</xsl:text>
                <xsl:value-of select="$break" />
                <xsl:text>$this->addElement($</xsl:text>
                <xsl:value-of select="@name" />
                <xsl:text>);</xsl:text>
                <xsl:value-of select="$break" />                             
            </xsl:for-each>
            <xsl:text>$submit = new Zend_Form_Element_Submit('submit');</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>$submit->setAttrib('id', 'submitbutton');</xsl:text>   
            <xsl:value-of select="$break" />
            <xsl:text>$this->addElement($submit);</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>}</xsl:text>
        </xsl:result-document>
    </xsl:template>
    
    
	
	<xsl:template name="getSQLType">
	   <xsl:param name="aorType"></xsl:param>
	   <xsl:choose>
	       <xsl:when test="$aorType = 'String'">
	           <xsl:text>VARCHAR(100)</xsl:text>
	       </xsl:when>
	       <xsl:when test="$aorType = 'Integer'">
               <xsl:text>INT(10)</xsl:text>
           </xsl:when>
           <xsl:when test="$aorType = 'Float'">
               <xsl:text>FLOAT</xsl:text>
           </xsl:when>
           <xsl:when test="$aorType = 'Boolean'">
               <xsl:text>BOOL</xsl:text>
           </xsl:when>
           <xsl:when test="$aorType = 'Date'">
               <xsl:text>DATE</xsl:text>
           </xsl:when>
           <xsl:when test="$aorType = 'Time'">
               <xsl:text>TIME</xsl:text>
           </xsl:when>
           <xsl:when test="$aorType = 'Datetime'">
               <xsl:text>DATETIME</xsl:text>
           </xsl:when>   
	   </xsl:choose>
	</xsl:template>
	<xsl:template name="getForeignKeys">
	   <xsl:param name="table"></xsl:param>
	   <xsl:for-each select="//aorml:BiNavigableAssociation[@domain=$table]
	       [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']
	       [@rangeMultiplicity='1']|//aorml:NavigableAssociation[@domain=$table]
           [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']
           [@rangeMultiplicity='1']">
	       <xsl:variable name="foreignTable" select="@range"></xsl:variable>
           <xsl:variable name="roleName" select="@rangeRoleName" />
	       <xsl:variable name="foreignPrimary" select="//aorml:ObjectType[@name=$foreignTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>
	       <xsl:variable name="foreignPrimaryType">
	           <xsl:call-template name="getSQLType">
	               <xsl:with-param name="aorType" select="//aorml:ObjectType[@name=$foreignTable]/aorml:Attribute/@type[../@isStandardID = 'true']"></xsl:with-param>
	           </xsl:call-template>
	       </xsl:variable>
	       <xsl:text>, `</xsl:text>
	       <xsl:value-of select="$roleName"></xsl:value-of>
	       <xsl:if test="not($roleName)">
                <xsl:value-of select="aorml:lowerFirst($foreignTable)"/>
           </xsl:if>
	       <xsl:text>` </xsl:text>
	       <xsl:value-of select="$foreignPrimaryType"></xsl:value-of>
	   </xsl:for-each>
	   <xsl:for-each select="//aorml:BiNavigableAssociation[@range=$table]
	       [@domainMultiplicity='1']
	       [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']
	       | //aorml:NavigableAssociation[@range=$table]
           [@domainMultiplicity='1']
           [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']">
           <xsl:variable name="foreignTable" select="@domain"></xsl:variable>
           <xsl:variable name="roleName" select="@startRoleName" />
          
           <xsl:variable name="foreignPrimary" select="//aorml:ObjectType[@name=$foreignTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>
           <xsl:variable name="foreignPrimaryType">
               <xsl:call-template name="getSQLType">
                   <xsl:with-param name="aorType" select="//aorml:ObjectType[@name=$foreignTable]/aorml:Attribute/@type[../@isStandardID = 'true']"></xsl:with-param>
               </xsl:call-template>
           </xsl:variable>
           <xsl:text>, `</xsl:text>
           <xsl:value-of select="$roleName"></xsl:value-of>
           <xsl:if test="not($roleName)">
                <xsl:value-of select="aorml:lowerFirst($foreignTable)"/>
           </xsl:if>
           <xsl:text>` </xsl:text>
           <xsl:value-of select="$foreignPrimaryType"></xsl:value-of>
       </xsl:for-each>
       <!-- Foreign Keys with references-->
       <xsl:for-each select="//aorml:BiNavigableAssociation[@domain=$table]
            [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']
            [@rangeMultiplicity='1']|//aorml:NavigableAssociation[@domain=$table]
            [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']
            [@rangeMultiplicity='1']">
           <xsl:variable name="foreignTable" select="@range"></xsl:variable>
           <xsl:variable name="roleName" select="@rangeRoleName" />
           <xsl:variable name="foreignPrimary" select="//aorml:ObjectType[@name=$foreignTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>
           <xsl:text>, FOREIGN KEY (`</xsl:text>
           <xsl:value-of select="$roleName"></xsl:value-of>
           <xsl:if test="not($roleName)">
                <xsl:value-of select="aorml:lowerFirst($foreignTable)"/>
           </xsl:if>
           <xsl:text>`) REFERENCES `</xsl:text>
           <xsl:value-of select="$foreignTable"></xsl:value-of>
           <xsl:text>` (`</xsl:text>
           <xsl:value-of select="$foreignPrimary"></xsl:value-of>
           <xsl:text>`)</xsl:text>
       </xsl:for-each>
       <xsl:for-each select="//aorml:BiNavigableAssociation[@range=$table]
            [@domainMultiplicity='1']
            [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']
            | //aorml:NavigableAssociation[@range=$table]
            [@domainMultiplicity='1']
            [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']">
           <xsl:variable name="foreignTable" select="@domain"></xsl:variable>
           <xsl:variable name="roleName" select="@startRoleName" />
           <xsl:variable name="foreignPrimary" select="//aorml:ObjectType[@name=$foreignTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>
           <xsl:text>, FOREIGN KEY (`</xsl:text>
           <xsl:value-of select="$roleName"></xsl:value-of>
           <xsl:if test="not($roleName)">
                <xsl:value-of select="aorml:lowerFirst($foreignTable)"/>
           </xsl:if>
           <xsl:text>`) REFERENCES `</xsl:text>
           <xsl:value-of select="$foreignTable"></xsl:value-of>
           <xsl:text>` (`</xsl:text>
           <xsl:value-of select="$foreignPrimary"></xsl:value-of>
           <xsl:text>`)</xsl:text>           
       </xsl:for-each>
	</xsl:template>
	
	<xsl:template name="dependentTables">
	   <xsl:param name="table"></xsl:param>	   
	   <xsl:text>protected $_dependentTables = array(</xsl:text>
	   <xsl:for-each select="//aorml:BiNavigableAssociation[@range=$table]
	   [@rangeMultiplicity='1']
	   [@domainMultiplicity='*' or @domainMultiplicity='0..1']
	   | //aorml:NavigableAssociation[@range=$table]
       [@rangeMultiplicity='1']
       [@domainMultiplicity='*' or @domainMultiplicity='0..1']">
	       <xsl:text>'Application_Model_DbTable_</xsl:text>
	       <xsl:value-of select="@domain"></xsl:value-of>
	       <xsl:text>',</xsl:text>
	   </xsl:for-each>
	   <xsl:for-each select="//aorml:BiNavigableAssociation[@domain=$table]
	   [@domainMultiplicity='1']
	   [@rangeMultiplicity='*' or @rangeMultiplicity='0..1']
	   | //aorml:NavigableAssociation[@domain=$table]
       [@domainMultiplicity='1']
       [@rangeMultiplicity='*' or @rangeMultiplicity='0..1']">
           <xsl:text>'Application_Model_DbTable_</xsl:text>
           <xsl:value-of select="@range"></xsl:value-of>
           <xsl:text>',</xsl:text>
       </xsl:for-each>
	   <xsl:text>);</xsl:text>
	</xsl:template>
	
    <xsl:template name="getManyToManySQL">
        <xsl:for-each select="//aorml:BiNavigableAssociation
            [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']
            [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']
            |//aorml:NavigableAssociation
            [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']
            [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']">
            <xsl:variable name="domainTable" select="@domain"></xsl:variable>
            <xsl:variable name="rangeTable" select="@range"></xsl:variable>
            <xsl:text>CREATE TABLE IF NOT EXISTS `</xsl:text>
            <xsl:value-of select="@name"></xsl:value-of>
            <xsl:if test="not(@name)">
                <xsl:value-of select="@domain"></xsl:value-of>
                <xsl:value-of select="@range"></xsl:value-of>
            </xsl:if>            
            <xsl:text>`(`</xsl:text>
            <xsl:variable name="domainPrimary" select="//aorml:ObjectType[@name=$domainTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>
            <xsl:variable name="domainPrimaryType">
                <xsl:call-template name="getSQLType">
                    <xsl:with-param name="aorType" select="//aorml:ObjectType[@name=$domainTable]/aorml:Attribute/@type[../@isStandardID = 'true']"></xsl:with-param>
                </xsl:call-template>
            </xsl:variable>
            <xsl:variable name="rangePrimary" select="//aorml:ObjectType[@name=$rangeTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>
            <xsl:variable name="rangePrimaryType">
                <xsl:call-template name="getSQLType">
                    <xsl:with-param name="aorType" select="//aorml:ObjectType[@name=$rangeTable]/aorml:Attribute/@type[../@isStandardID = 'true']"></xsl:with-param>
                </xsl:call-template>
            </xsl:variable>
            <xsl:value-of select="@domainRoleName"></xsl:value-of>
            <xsl:if test="not(@domainRoleName)">
                <xsl:value-of select="aorml:lowerFirst(@domain)"></xsl:value-of>
            </xsl:if>
            <xsl:text>` </xsl:text>
            <xsl:value-of select="$domainPrimaryType"></xsl:value-of>
            <xsl:text>, `</xsl:text>
            <xsl:value-of select="@rangeRoleName"></xsl:value-of>
            <xsl:if test="not(@rangeRoleName)">
                <xsl:value-of select="aorml:lowerFirst(@range)"></xsl:value-of>
            </xsl:if>
            <xsl:text>` </xsl:text>
            <xsl:value-of select="$rangePrimaryType"></xsl:value-of>
            <xsl:text>, PRIMARY KEY (`</xsl:text>
            <xsl:value-of select="@domainRoleName"></xsl:value-of>
            <xsl:if test="not(@domainRoleName)">
                <xsl:value-of select="aorml:lowerFirst(@domain)"/>
            </xsl:if>
            <xsl:text>`, `</xsl:text>
           <xsl:value-of select="@rangeRoleName"></xsl:value-of>
           <xsl:if test="not(@rangeRoleName)">
                <xsl:value-of select="aorml:lowerFirst(@range)"/>
           </xsl:if>
            <xsl:text>`), FOREIGN KEY (`</xsl:text>
           <xsl:value-of select="@domainRoleName"></xsl:value-of>
           <xsl:if test="not(@domainRoleName)">
                <xsl:value-of select="aorml:lowerFirst(@domain)"/>
           </xsl:if>           
           <xsl:text>`) REFERENCES `</xsl:text>
           <xsl:value-of select="@domain"></xsl:value-of>
           <xsl:text>` (`</xsl:text>
           <xsl:value-of select="$domainPrimary"></xsl:value-of>
           <xsl:text>`) ON DELETE CASCADE</xsl:text>           
            <xsl:text>, FOREIGN KEY (`</xsl:text>
           <xsl:value-of select="@rangeRoleName"></xsl:value-of>
           <xsl:if test="not(@rangeRoleName)">
                <xsl:value-of select="aorml:lowerFirst(@range)"/>
           </xsl:if>
           <xsl:text>`) REFERENCES `</xsl:text>
           <xsl:value-of select="@range"></xsl:value-of>
           <xsl:text>` (`</xsl:text>
           <xsl:value-of select="$rangePrimary"></xsl:value-of>
           <xsl:text>`) ON DELETE CASCADE ) ENGINE=INNODB;</xsl:text>
        </xsl:for-each>
    </xsl:template>
	
	<xsl:template name="referenceMap">
        <xsl:param name="table"></xsl:param>
        <xsl:text>protected $_referenceMap = array(</xsl:text>
        <xsl:for-each select="//aorml:BiNavigableAssociation[@domain=$table]
        [@rangeMultiplicity='1']
        [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']
        |//aorml:NavigableAssociation[@domain=$table]
        [@rangeMultiplicity='1']
        [@domainMultiplicity='*' or @domainMultiplicity='0..1' or @domainMultiplicity='1..*']">
            <xsl:variable name="foreignTable" select="@range"></xsl:variable>
            <xsl:variable name="roleName" select="@rangeRoleName"></xsl:variable>
            <xsl:variable name="foreignPrimary" select="//aorml:ObjectType[@name=$foreignTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>
            <xsl:text>'</xsl:text>
            <xsl:value-of select="@name"></xsl:value-of>
            <xsl:if test="not(@name)">
                <xsl:value-of select="aorml:lowerFirst($foreignTable)"/>
            </xsl:if>
            <xsl:text>' => array(</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>'columns' => '</xsl:text>
            <xsl:value-of select="$roleName"></xsl:value-of>
            <xsl:if test="not($roleName)">
                <xsl:value-of select="aorml:lowerFirst($foreignTable)"/>
            </xsl:if>
            <xsl:text>', 'refTableClass' => 'Application_Model_DbTable_</xsl:text>
            <xsl:value-of select="$foreignTable"></xsl:value-of>
            <xsl:text>', 'refColumn' => '</xsl:text>
            <xsl:value-of select="$foreignPrimary"></xsl:value-of>
            <xsl:text>' ),</xsl:text>
            <xsl:value-of select="$break" />
        </xsl:for-each>
        <xsl:for-each select="//aorml:BiNavigableAssociation[@range=$table]
        [@domainMultiplicity='1']
        [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']
        | //aorml:NavigableAssociation[@range=$table]
        [@domainMultiplicity='1']
        [@rangeMultiplicity='*' or @rangeMultiplicity='0..1' or @rangeMultiplicity='1..*']">
            <xsl:variable name="foreignTable" select="@domain"></xsl:variable>
            <xsl:variable name="roleName" select="@domainRoleName"></xsl:variable>
            <xsl:variable name="foreignPrimary" select="//aorml:ObjectType[@name=$foreignTable]/aorml:Attribute/@name[../@isStandardID = 'true']"></xsl:variable>
            <xsl:text>'</xsl:text>
            <xsl:value-of select="@name"></xsl:value-of>
            <xsl:if test="not(@name)">
                <xsl:value-of select="aorml:lowerFirst($foreignTable)"/>
            </xsl:if>
            <xsl:text>' => array(</xsl:text>
            <xsl:value-of select="$break" />
            <xsl:text>'columns' => '</xsl:text>
            <xsl:value-of select="$roleName"></xsl:value-of>
            <xsl:if test="not($roleName)">
                <xsl:value-of select="aorml:lowerFirst($foreignTable)"/>
            </xsl:if>
            <xsl:text>', 'refTableClass' => 'Application_Model_DbTable_</xsl:text>
            <xsl:value-of select="$foreignTable"></xsl:value-of>
            <xsl:text>', 'refColumn' => '</xsl:text>
            <xsl:value-of select="$foreignPrimary"></xsl:value-of>
            <xsl:text>' ),</xsl:text>
            <xsl:value-of select="$break" /> 
        </xsl:for-each>
        <xsl:text>);</xsl:text>
    </xsl:template>
</xsl:stylesheet>